/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.guarantor;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.guarantor.Guarantor;
import com.jeesite.modules.base.service.guarantor.GuarantorService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 担保方Controller
 * @author lv
 * @version 2021-02-24
 */
@Controller
@RequestMapping(value = "${adminPath}/base/guarantor/guarantor")
public class GuarantorController extends BaseController {

    @Autowired
    private GuarantorService guarantorService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public Guarantor get(String guarantorId, boolean isNewRecord) {
        return guarantorService.get(guarantorId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("base:guarantor:view")
    @RequestMapping(value = {"list", ""})
    public String list(Guarantor guarantor, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("guarantor", guarantor);
        return "modules/base/guarantor/guarantorList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:guarantor:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<Guarantor> listData(Guarantor guarantor, HttpServletRequest request, HttpServletResponse response) {
        guarantor.setPage(new Page<>(request, response));
        Page<Guarantor> page = guarantorService.findPage(guarantor);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:guarantor:view")
    @RequestMapping(value = "form")
    public String form(Guarantor guarantor, Model model) {
        model.addAttribute("listUser",userService.findList(new User()));
        model.addAttribute("guarantor", guarantor);
        return "modules/base/guarantor/guarantorForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:guarantor:view")
    @RequestMapping(value = "detail")
    public String detail(Guarantor guarantor, Model model) {
        model.addAttribute("listUser",userService.findList(new User()));
        model.addAttribute("guarantor", guarantor);
        return "modules/base/guarantor/guarantorDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:guarantor:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated Guarantor guarantor) {
        guarantorService.save(guarantor);
        return renderResult(Global.TRUE, text("保存担保方成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("base:guarantor:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(Guarantor guarantor) {
        guarantorService.delete(guarantor);
        return renderResult(Global.TRUE, text("删除担保方成功！"));
    }

}